/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Dialog_TimFile.java
 *
 * Created on Apr 13, 2009, 3:48:12 PM
 */

package QuanLyFile;

import DuyetFile.BangDuyetFile;
import DuyetFile.EventListener_ClickChuotVaoBangDuyetFile;
import doan_totalcommander_002.jEnum_CacEnumTrongBai;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JOptionPane;
import javax.swing.SwingWorker;
import javax.swing.table.DefaultTableModel;

/**
 * tham khảo http://java.sun.com/docs/books/tutorial/uiswing/examples/components/ProgressBarDemoProject/src/components/ProgressBarDemo.java
 * @author Administrator
 */
public class Dialog_TimFile extends javax.swing.JFrame implements ActionListener,
                                        PropertyChangeListener{
    Task task = new Task();
    private BangDuyetFile bangDuyetFile;
    /** Creates new form Dialog_TimFile */
    public Dialog_TimFile(java.awt.Frame parent, boolean modal) {
        super();
        initComponents();
        jTable_TimDuoc.setAutoCreateRowSorter(true);
        //this.setAlwaysOnTop(true);
        DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
        jTextField_NgayCuoi.setText(dateFormat.format(new Date()).toString());
        for (File phanVung : File.listRoots()){
            jComboBox_ViTriDuyet.addItem(phanVung.getPath());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jPanel4 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jTextField_TenFile = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextField_NgayDau = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jTextField_KichThuocDau = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jButton_Tim = new javax.swing.JButton();
        jComboBox_LoaiKichThuocKetThuc = new javax.swing.JComboBox();
        jComboBox_LoaiKichThuocBatDau = new javax.swing.JComboBox();
        jTextField_KichThuocCuoi = new javax.swing.JTextField();
        jCheckBox_TrungKhopTatCa = new javax.swing.JCheckBox();
        jSeparator1 = new javax.swing.JSeparator();
        jTextField_NgayCuoi = new javax.swing.JTextField();
        jCheckBox_NgayThayDoiCuoi = new javax.swing.JCheckBox();
        jCheckBox_KichThuoc = new javax.swing.JCheckBox();
        jLabel7 = new javax.swing.JLabel();
        jComboBox_ViTriDuyet = new javax.swing.JComboBox();
        jButton_Ngung = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel_DuongDanDangTim = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable_TimDuoc = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel3.setLayout(new java.awt.BorderLayout());

        jPanel1.setPreferredSize(new java.awt.Dimension(806, 120));

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder("Các tiêu chí tìm kiếm"));

        jLabel6.setText("Tên file:");

        jLabel2.setText("Ngày thay đổi cuối từ:");

        jTextField_NgayDau.setText("1/1/1970");

        jLabel4.setText("đến:");

        jLabel1.setText("Kích thước từ:");

        jTextField_KichThuocDau.setText("0");

        jLabel5.setText("đến:");

        jButton_Tim.setText("Tìm");
        jButton_Tim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_TimActionPerformed(evt);
            }
        });

        jComboBox_LoaiKichThuocKetThuc.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Byte", "KB", "MB", "GB" }));

        jComboBox_LoaiKichThuocBatDau.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Byte", "KB", "MB", "GB" }));

        jTextField_KichThuocCuoi.setText("0");

        jCheckBox_TrungKhopTatCa.setSelected(true);
        jCheckBox_TrungKhopTatCa.setText("Trùng khớp tất cả");

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);

        jCheckBox_NgayThayDoiCuoi.setSelected(true);
        jCheckBox_NgayThayDoiCuoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_NgayThayDoiCuoiActionPerformed(evt);
            }
        });

        jCheckBox_KichThuoc.setSelected(true);
        jCheckBox_KichThuoc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_KichThuocActionPerformed(evt);
            }
        });

        jLabel7.setText("Trong:");

        jComboBox_ViTriDuyet.setEditable(true);

        jButton_Ngung.setText("Ngưng");
        jButton_Ngung.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_NgungActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jCheckBox_TrungKhopTatCa)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton_Tim, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton_Ngung))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel6)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBox_ViTriDuyet, 0, 199, Short.MAX_VALUE)
                            .addComponent(jTextField_TenFile, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 2, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jCheckBox_NgayThayDoiCuoi)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel2))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jCheckBox_KichThuoc)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel1)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                        .addComponent(jTextField_NgayDau, javax.swing.GroupLayout.DEFAULT_SIZE, 131, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jTextField_KichThuocDau, javax.swing.GroupLayout.DEFAULT_SIZE, 78, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox_LoaiKichThuocBatDau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jTextField_KichThuocCuoi, javax.swing.GroupLayout.DEFAULT_SIZE, 75, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboBox_LoaiKichThuocKetThuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTextField_NgayCuoi, javax.swing.GroupLayout.DEFAULT_SIZE, 128, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel6, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextField_TenFile, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBox_ViTriDuyet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jTextField_NgayCuoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox_NgayThayDoiCuoi)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jTextField_NgayDau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jCheckBox_TrungKhopTatCa)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(jTextField_KichThuocCuoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox_LoaiKichThuocKetThuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5)
                            .addComponent(jTextField_KichThuocDau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBox_LoaiKichThuocBatDau, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addComponent(jCheckBox_KichThuoc))
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton_Tim)
                        .addComponent(jButton_Ngung))))
            .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel3.add(jPanel1, java.awt.BorderLayout.PAGE_START);

        jPanel2.setPreferredSize(new java.awt.Dimension(400, 30));

        jLabel_DuongDanDangTim.setText("Đang tìm trong:");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel_DuongDanDangTim)
                .addContainerGap(623, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel_DuongDanDangTim)
                .addContainerGap())
        );

        jPanel3.add(jPanel2, java.awt.BorderLayout.PAGE_END);

        jTable_TimDuoc.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Tên", "Loại", "Đường dẫn", "Size (bytes)"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Long.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable_TimDuoc.setColumnSelectionAllowed(true);
        jTable_TimDuoc.getTableHeader().setReorderingAllowed(false);
        jTable_TimDuoc.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable_TimDuocMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jTable_TimDuoc);
        jTable_TimDuoc.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        jTable_TimDuoc.getColumnModel().getColumn(1).setMinWidth(64);
        jTable_TimDuoc.getColumnModel().getColumn(1).setPreferredWidth(64);
        jTable_TimDuoc.getColumnModel().getColumn(1).setMaxWidth(64);
        jTable_TimDuoc.getColumnModel().getColumn(3).setMinWidth(96);
        jTable_TimDuoc.getColumnModel().getColumn(3).setPreferredWidth(96);
        jTable_TimDuoc.getColumnModel().getColumn(3).setMaxWidth(96);

        jPanel3.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel3, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jButton_TimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_TimActionPerformed
        jButton_Tim.setEnabled(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        //Instances of javax.swing.SwingWorker are not reusuable, so
        //we create new instances as needed.
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
}//GEN-LAST:event_jButton_TimActionPerformed
    private void jCheckBox_NgayThayDoiCuoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_NgayThayDoiCuoiActionPerformed
        // TODO add your handling code here:
        jTextField_NgayDau.setEnabled(jCheckBox_NgayThayDoiCuoi.isSelected());
        jTextField_NgayCuoi.setEnabled(jCheckBox_NgayThayDoiCuoi.isSelected());
    }//GEN-LAST:event_jCheckBox_NgayThayDoiCuoiActionPerformed
    private void jCheckBox_KichThuocActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox_KichThuocActionPerformed
        // TODO add your handling code here:
        jTextField_KichThuocDau.setEnabled(jCheckBox_KichThuoc.isSelected());
        jTextField_KichThuocCuoi.setEnabled(jCheckBox_KichThuoc.isSelected());
}//GEN-LAST:event_jCheckBox_KichThuocActionPerformed

    private void jButton_NgungActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton_NgungActionPerformed
        // TODO add your handling code here:
        task.cancel(true);
}//GEN-LAST:event_jButton_NgungActionPerformed

    private void jTable_TimDuocMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable_TimDuocMouseClicked
        try {
            // TODO add your handling code here:
            if (evt.getButton() != 1 || evt.getClickCount() == 1) {
                //Nếu không click chuột trái hoặc mới click 1 lần
                return;
            }
            int iDongDuocChon = jTable_TimDuoc.getSelectedRow();
            //Nếu loại file là folder thì phát sinh sự kiện để bảng duyệt file bắt với tham số là đường dẫn

            if (jTable_TimDuoc.getValueAt(iDongDuocChon, 1).equals("Folder")) {
                phatSinhSuKien_ClickChuotVaoBangDuyetFile(jTable_TimDuoc.getValueAt(iDongDuocChon, 0).toString(), 2);
            } else {//Nếu là file thì thử mở với chương trình mặc định
                Desktop.getDesktop().open(new File(jTable_TimDuoc.getValueAt(iDongDuocChon, 2) + "\\" +
                        jTable_TimDuoc.getValueAt(iDongDuocChon, 0) + jTable_TimDuoc.getValueAt(iDongDuocChon, 1)));
                //Tổng hợp cột 2 (đường dẫn), 0 (tên) và 1 (loại) lại sẽ được đường dẫn file hoàn chỉnh
            }
        } catch (IOException ex) {
            Logger.getLogger(Dialog_TimFile.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jTable_TimDuocMouseClicked
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Dialog_TimFile dialog = new Dialog_TimFile(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_Ngung;
    private javax.swing.JButton jButton_Tim;
    private javax.swing.JCheckBox jCheckBox_KichThuoc;
    private javax.swing.JCheckBox jCheckBox_NgayThayDoiCuoi;
    private javax.swing.JCheckBox jCheckBox_TrungKhopTatCa;
    private javax.swing.JComboBox jComboBox_LoaiKichThuocBatDau;
    private javax.swing.JComboBox jComboBox_LoaiKichThuocKetThuc;
    private javax.swing.JComboBox jComboBox_ViTriDuyet;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel_DuongDanDangTim;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTable_TimDuoc;
    private javax.swing.JTextField jTextField_KichThuocCuoi;
    private javax.swing.JTextField jTextField_KichThuocDau;
    private javax.swing.JTextField jTextField_NgayCuoi;
    private javax.swing.JTextField jTextField_NgayDau;
    private javax.swing.JTextField jTextField_TenFile;
    // End of variables declaration//GEN-END:variables
    public void actionPerformed(ActionEvent e) {
        // TODO add your handling code here:
        jButton_Tim.setEnabled(false);
        setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
        //Instances of javax.swing.SwingWorker are not reusuable, so
        //we create new instances as needed.
        task = new Task();
        task.addPropertyChangeListener(this);
        task.execute();
    }
    public void propertyChange(PropertyChangeEvent evt) {
        //int i = 0;
        if ("STARTED" == evt.getNewValue().toString()) {
            //int progress = (Integer) evt.getNewValue();
            //Object[] dongDuLieu = {evt., strLoai, strDuongDan, lSize};
            DefaultTableModel model = (DefaultTableModel) jTable_TimDuoc.getModel();
            model.setRowCount(0);
            jTable_TimDuoc.setModel(model);
            //taskOutput.append(task.f);
        }
        if ("File" == evt.getPropertyName()) {
            
            File file = (File) evt.getNewValue();
            String strTen = file.getName();
            String strLoai = "";
            String strDuongDan = file.getAbsolutePath();
            long lSize = file.length();
            if (strTen.contains(".")){
                strLoai = strTen.substring(strTen.lastIndexOf("."), strTen.length());
                strTen = strTen.substring(0, strTen.lastIndexOf("."));
            }

            Object[] dongDuLieu = {strTen, strLoai, strDuongDan, lSize};
            DefaultTableModel model = (DefaultTableModel) jTable_TimDuoc.getModel();
            model.addRow(dongDuLieu);
            jTable_TimDuoc.setModel(model);
            //jTable_TimDuoc.getModel()
        }
        if ("Folder" == evt.getPropertyName()) {
            jLabel_DuongDanDangTim.setText("Đang tìm trong: " + evt.getNewValue().toString() + "\n");

            String regex = evt.getOldValue().toString().replace(".", "\\.");
            regex = regex.replace("*", ".*");
            regex = regex.replace("?", ".?");
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(new File(evt.getNewValue().toString()).getName());
            if (!matcher.find())
            //if(!pathname.getName().contains(tenFile))
                return;

            Object[] dongDuLieu = {evt.getNewValue(), "Folder", "", (long)0};
            DefaultTableModel model = (DefaultTableModel) jTable_TimDuoc.getModel();
            model.addRow(dongDuLieu);
            jTable_TimDuoc.setModel(model);
        }
    }

    /**
     * @param bangDuyetFile the bangDuyetFile to set
     */
    public void setBangDuyetFile(BangDuyetFile bangDuyetFile) {
        this.bangDuyetFile = bangDuyetFile;
    }
    //Các hàm sau phục vụ cho việc gởi sự kiện click chuột vào bảng ra ngoài (tham khảo từ nhiều nguồn trên mạng)
    //http://www.exampledepot.com/egs/java.util/CustEvent.html
    // Tạo một listener list
    protected javax.swing.event.EventListenerList listenerList =
            new javax.swing.event.EventListenerList();

    /**
     * Phát sinh sử kiện click chuột vào bảng
     * @param evt tham số cho sự kiện click chuột vào bảng (ở đây là tên của file đang được chọn)
     * @param iSoLanClick số lần click chuột
     */
    // This private class is used to fire MyEvents
    void phatSinhSuKien_ClickChuotVaoBangDuyetFile(String evt, int iSoLanClick) {
        Object[] listeners = listenerList.getListenerList();
        // Each listener occupies two elements - the first is the listener class
        // and the second is the listener instance
        for (int i = 0; i < listeners.length; i += 2) {
            if (listeners[i] == EventListener_ClickChuotVaoBangDuyetFile.class) {
                ((EventListener_ClickChuotVaoBangDuyetFile) listeners[i + 1]).Event_ClickChuotVaoBangDuyetFile_Occurred(evt, iSoLanClick);
            }
        }
    }

    /**
     * Đăng ký sự kiện cho classes
     * @param listener  Sự kiện cần đăng ký
     */
    public void themEventListener_ClickChuotVaoBangDuyetFile(EventListener_ClickChuotVaoBangDuyetFile listener) {
        listenerList.add(EventListener_ClickChuotVaoBangDuyetFile.class, listener);
    }

    /**
     * Gỡ bỏ sự kiện khỏi classes
     * @param listener  Sự kiện cần gỡ bỏ
     */
    public void boEventListener_ClickChuotVaoBangDuyetFile(EventListener_ClickChuotVaoBangDuyetFile listener) {
        listenerList.remove(EventListener_ClickChuotVaoBangDuyetFile.class, listener);
    }
    class Task extends SwingWorker<Void, Void> {
        public void DuyetFile(String duongDan, BoLocFile filter){
            
            File files = new File(duongDan);
            firePropertyChange("Folder", filter.getTenFile(), files.getAbsolutePath());
            for (File file : files.listFiles(filter)){
                try {
                    Thread.sleep(1L);
                } catch (InterruptedException ignore) {
                    Thread.currentThread().stop();
                }
                if (file.isDirectory()){
                    DuyetFile(file.getAbsolutePath(), filter);
                }
                else{
                    firePropertyChange("File", "", file);
                }
            }
        }
        /*
         * Main task. Executed in background thread.
         */
        @Override
        public Void doInBackground() {
            long l_NgayBatDau, l_NgayKetThuc, l_KichThuocBatDau, l_KichThuocKetThuc;
            //<editor-fold defaultstate="collapsed" desc="Bắt lỗi input và 4 thuộc tính đầu">
            if (jCheckBox_NgayThayDoiCuoi.isSelected()){
                DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT);
                try{l_NgayBatDau = dateFormat.parse(jTextField_NgayDau.getText()).getTime();}
                catch(Exception ex){
                    l_NgayBatDau = 0;
                    JOptionPane.showMessageDialog(null, "Thông tin ngày bắt đầu không hợp lệ sẽ bỏ qua!");
                }
                try{l_NgayKetThuc = dateFormat.parse(jTextField_NgayCuoi.getText()).getTime();}
                catch(Exception ex){
                    l_NgayKetThuc = 0;
                    JOptionPane.showMessageDialog(null, "Thông tin ngày kết thúc không hợp lệ sẽ bỏ qua!");
                }
            }
            else{
                l_NgayBatDau = l_NgayKetThuc = 0;
            }

            if (jCheckBox_KichThuoc.isSelected()){
                try{l_KichThuocBatDau = Integer.parseInt(jTextField_KichThuocDau.getText());}
                catch(Exception ex){
                    l_KichThuocBatDau = 0;
                    JOptionPane.showMessageDialog(null, "Thông tin kích thước bắt đầu không hợp lệ bỏ qua");
                }
                try{l_KichThuocKetThuc = Integer.parseInt(jTextField_KichThuocCuoi.getText());}
                catch(Exception ex){
                    l_KichThuocKetThuc = 0;
                    JOptionPane.showMessageDialog(null, "Thông tin kích thước không hợp lệ sẽ bỏ qua!");
                }
            }
            else{
                l_KichThuocBatDau = l_KichThuocKetThuc = 0;
            }
            //</editor-fold>
            String loaiKichThuoc = jComboBox_LoaiKichThuocBatDau.getSelectedItem().toString();
            if (loaiKichThuoc.equalsIgnoreCase("KB"))
                l_KichThuocBatDau *= jEnum_CacEnumTrongBai.KB.value();
            else if(loaiKichThuoc.equalsIgnoreCase("MB"))
                l_KichThuocBatDau *= jEnum_CacEnumTrongBai.MB.value();
            else if(loaiKichThuoc.equalsIgnoreCase("GB"))
                l_KichThuocBatDau *= jEnum_CacEnumTrongBai.GB.value();

            loaiKichThuoc = jComboBox_LoaiKichThuocKetThuc.getSelectedItem().toString();
            if (loaiKichThuoc.equalsIgnoreCase("KB"))
                l_KichThuocKetThuc *= jEnum_CacEnumTrongBai.KB.value();
            else if(loaiKichThuoc.equalsIgnoreCase("MB"))
                l_KichThuocKetThuc *= jEnum_CacEnumTrongBai.MB.value();
            else if(loaiKichThuoc.equalsIgnoreCase("GB"))
                l_KichThuocKetThuc *= jEnum_CacEnumTrongBai.GB.value();

            BoLocFile boLocFile = new BoLocFile(l_NgayBatDau, l_NgayKetThuc,
                    l_KichThuocBatDau, l_KichThuocKetThuc,
                    jTextField_TenFile.getText(), jCheckBox_TrungKhopTatCa.isSelected());

            DuyetFile(jComboBox_ViTriDuyet.getSelectedItem().toString(), boLocFile);
            return null;
        }

        /*
         * Executed in event dispatching thread
         */
        @Override
        public void done() {
            Toolkit.getDefaultToolkit().beep();
            jButton_Tim.setEnabled(true);
            setCursor(null); //turn off the wait cursor
            jLabel_DuongDanDangTim.setText("Số file tìm được: " + jTable_TimDuoc.getRowCount());
            setProgress(100);
        }
    }
}
class BoLocFile implements FileFilter{
    long ngayBatDau;
    long ngayKetThuc;
    long kichThuocBatDau;
    long kichThuocKetThuc;
    private String tenFile;
    boolean khopTatCa;
    
    public boolean accept(File pathname) {
        //Nếu ngày thay đổi cuối không thuộc khoảng phù hợp và phải trùng khớp tất cả
        //Nếu các giá trị kết thúc bằng 0 thì bỏ qua
        //Trả về false
        if (pathname.isDirectory())
            return true;
        if (pathname.lastModified() < ngayBatDau
                || (pathname.lastModified() > ngayKetThuc  && 0 != ngayKetThuc)&& khopTatCa)
            return false;
        //Tương tự cho kích thước
        if (pathname.length() < kichThuocBatDau
                || (pathname.length() > kichThuocKetThuc && 0 != kichThuocKetThuc) && khopTatCa)
            return false;
        //Nếu đã đạt được cả hai
        String regex = getTenFile().replace(".", "\\.");
        regex = regex.replace("*", ".*");
        regex = regex.replace("?", ".?");
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(pathname.getName());
        if (!matcher.find())
        //if(!pathname.getName().contains(tenFile))
            return false;
        return true;
    }
    public BoLocFile (long l_NgayBatDau, long l_NgayKetThuc,
            long l_KichThuocBatDau, long l_KichThuocKetThuc, String str_TenFile, boolean b_KhopTatCa){

        ngayBatDau = l_NgayBatDau;
        ngayKetThuc = l_NgayKetThuc;
        kichThuocBatDau = l_KichThuocBatDau;
        kichThuocKetThuc = l_KichThuocKetThuc;
        tenFile = str_TenFile;
        khopTatCa = b_KhopTatCa;
    }

    /**
     * @return the tenFile
     */
    public String getTenFile() {
        return tenFile;
    }
}